from rest_framework.permissions import IsAdminUser
from rest_framework.viewsets import ModelViewSet

from goods.models import SPU
from meiduo_admin.serializers.skus import SPUSpecificationSerialzier
from meiduo_admin.serializers.spus import SPUGoodsSerialzier
from meiduo_admin.utils import PageNum
from rest_framework.response import Response

class SPUView(ModelViewSet):
    '''

    spu增删该查
    '''

    #指定序列化器
    serializer_class = SPUGoodsSerialzier
    #指定查询集
    queryset = SPU.objects.all()
    #指定分页器
    pagination_class = PageNum
    #指定权限
    permission_classes = [IsAdminUser]


    # 获取商品的相关规格信息
    def specs(self,reuqest,pk):
        # 根据pk值先查查询spu商品
        spu=SPU.objects.get(id=pk)
        # 根据spu商品对象返回规格和规格选贤
        spec=spu.specs.all()
        # 返回规格数据
        ser=SPUSpecificationSerialzier(spec,many=True)
        return Response(ser.data)